<?php

namespace frontendw\models;

/**
 * @author  shuang
 * @date    2016-9-6 13:02:13
 * @version V1.0
 * @desc    
 */
use common\models\UserProfile;
use frontendw\models\WB_UserAccountHy;
use frontendw\models\WB_Grade;
use frontendw\models\WB_UserDrawal;
use Yii;
use yii\db\Query;
use frontendw\models\WB_UserTaskOperate;

class WB_UserProfile extends UserProfile {
    /*
     * 查询用户基本信息
     * @params $userid
     * return array
     */

    public static function getInfo($userid) {
        $query = new Query();
        $query->from(WB_UserProfile::tableName() . " as a");
        $query->leftJoin(WB_UserAccountHy::tableName() . " as b", "a.userid=b.userid");
        $query->leftJoin(WB_Grade::tableName() . " as c", "a.gradeid=c.id");
        $query->select("a.username,b.amount,b.offline_amount,a.alipay,a.qq,a.truename");
        $query->where("a.userid=:userid", array(":userid" => $userid));
        $query->addSelect("(SELECT SUM(amount) FROM " . WB_UserDrawal::tableName() . " WHERE userid=$userid and status=2) AS payamount");
        $query->addSelect("(SELECT count(userid) FROM " . WB_UserProfile::tableName() . " WHERE invite_code='" . Yii::$app->user->identity->my_code . "') AS usercount");
        return $query->one();
    }

    /*
     * 下线信息
     */

    public static function getOfflineList() {
        $query = new Query();
        $query->from(WB_UserProfile::tableName() . " as a");
        $query->leftJoin(\common\models\User::tableName() . " as b", "a.userid=b.id");
        $query->leftJoin(WB_UserTaskOperate::tableName() . " as c", "a.userid=c.userid");
        $query->select("a.username,a.qq,b.last_login_at,b.created_at,count(c.id) as taskcount");
        $query->where("a.invite_code=:invite_code", array(":invite_code" => Yii::$app->user->identity->my_code));
        $query->groupBy("a.userid");
        $countQuery = clone $query;
        $pager = new \yii\data\Pagination(['totalCount' => $countQuery->count(), 'defaultPageSize' => 10]);
        $offset = Yii::$app->request->get("offset");
        $limit = Yii::$app->request->get("limit");
        $res = $query->offset($offset)->limit($limit)->all();
        return ["pager" => $pager, "data" => $res];
    }
    
    /*
     * 获得推广总数
     */
    public static function getInviteCount($code){
        return WB_UserProfile::find()->where("invite_code=:invite_code",[":invite_code"=>$code])->count();
    }

}
